﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Web.Security;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging; 
using SaleOffVn.BusinessEntities;
using SaleOffVn.DataAccess;

namespace SaleOffVn.BusinessLogic
{
    public class NewsManager
    {
        #region Public methods

        public static NewsInfo GetItemById(int newsId)
        {
            return NewsDB.GetItemById(newsId);
        }
        public static NewsCollection GetList(NewsInfo criteria)
        {
            NewsCollection collection = NewsDB.GetList(criteria);
            return collection;
        }

        public static int Add(NewsInfo news)
        {            
            int iID = 0;
            iID = NewsDB.Add(news);
            return iID;
        }

        public static int Update(NewsInfo news)
        {
            return NewsDB.Update(news);
        }
        
        public static void Delete(int newId)
        {
            NewsDB.Delete(newId);
        }

        public static DataTable GetAllNews(int isPublished)
        {
            DataTable listNews = new DataTable();
            listNews = NewsDB.GetAllNews(isPublished);
            return listNews;
        }

        public static void UploadFile(string directory, FileUpload fileUpload, int iNewsID)
        {            
            
            // Create a bitmap of the content of the fileUpload control in memory
            Bitmap originalBMP = new Bitmap(fileUpload.FileContent);

            // Calculate the new image dimensions
            int origWidth = originalBMP.Width;
            int origHeight = originalBMP.Height;
            int sngRatio = origWidth / origHeight;
            int newWidth = 100;
            if (sngRatio == 0)
                sngRatio = 1;
            int newHeight = newWidth / sngRatio;

            // Create a new bitmap which will hold the previous resized bitmap
            Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);

            // Create a graphic based on the new bitmap
            Graphics oGraphics = Graphics.FromImage(newBMP);
            // Set the properties for the new graphic file
            oGraphics.SmoothingMode = SmoothingMode.AntiAlias; 
            oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

            // Draw the new graphic based on the resized bitmap
            oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
            // Save the new graphic file to the server
            newBMP.Save(directory + iNewsID + ".gif");

            // Once finished with the bitmap objects, we deallocate them.
            originalBMP.Dispose();
            newBMP.Dispose();
            oGraphics.Dispose();
        }

        public static void DeleteFile(string strPathFile)
        {
            FileInfo file = new FileInfo(strPathFile);
            if (file.Exists)
            {
                File.Delete(strPathFile);
            }
            else
            {
                //throw new FileNotFoundException();
            }
        }

        public static NewsInfo GetPreviousNews(int iCurrNewsID)
        {
            NewsInfo newsinfo = null;
            newsinfo = NewsDB.GetPreviousNews(iCurrNewsID);
            return newsinfo;
        }

        public static NewsInfo GetNextNews(int iCurrNewsID)
        {
            NewsInfo newsinfo = null;
            newsinfo = NewsDB.GetNextNews(iCurrNewsID);
            return newsinfo;
        }

        #endregion
    }
}
